import CONSTANT from '@/common/constants.js'
import http from '@/common/http.js'
import util from '@/common/util.js'

export default {
  name: 'Hello',
  components: {},
  data () {
    return {
      siteId: '',
      studentId: '',
      spelName: '',
      area: '',
      term: '',
      weekDay: '',
      specialtyText: '专业',
      spelId: '',
      pageNo: 1,
      classNoShow: false,
      content: [],
      optionsArea: [],
      propsArea: {
        value: 'value',
        children: 'cities'
      },
      parentCodeArea: '0',
      optionsTerm: [],
      optionsSpelId: [],
      optionsWeekDay: [],
      selectedOptionsArea: [],
      selectedOptionsTerm: [],
      selectedOptionsSpelId: [],
      selectedOptionsWeekDay: []
    }
  },
  mounted () {
    let areaId = window.sessionStorage.getItem('areaId')
    let areaName = window.sessionStorage.getItem('areaName')
    let classNoFlag = window.sessionStorage.getItem('classNoFlag')
    if (classNoFlag == 1) {
      this.classNoShow = true
    }
    this.area = areaId
    this.spelName = areaName
    this.siteId = window.sessionStorage.getItem('siteId')
    if (this.siteId == '71F7B4B5ADF92308E0530100007F712B') {
      this.specialtyText = '学校';
    }
    this.studentId = window.sessionStorage.getItem('studentId')
    if (this.studentId == '-1') {
      this.studentId = ''
    }
    let isNetApply = window.sessionStorage.getItem('isNetApply')
    if (isNetApply == 1) {
      this.classList()
    }
    this.changeArea()
    this.termlist()
    //this.specialtylist()
    this.weeklist()
  },
  methods: {
    changeAreaSelected(val) {
      var _this = this
      _this.area = val[0]
      _this.classList()
      for (var n = 0; n < _this.optionsSpelId.length; n++) {
        if (val[0] == _this.optionsSpelId[n].value) {
          _this.spelName = _this.optionsSpelId[n].label
          return
        }
      }
    },
    changeTermSelected (val) {
      var _this = this
      _this.term = val[0]
      _this.pageNo = 1
      _this.classList()
    },
    changeSpelIdSelected (val) {
      var _this = this
      _this.spelId = val[1]
      _this.pageNo = 1
      _this.classList()
    },
    changeWeekDaySelected (val) {
      var _this = this
      _this.weekDay = val[0]
      _this.pageNo = 1
      _this.classList()
    },
    lookmore () {
      var _this = this
      if (_this.pageNo >= 1) {
        _this.pageNo++
        this.classList()
      }
    },
    classListResultHandle(data) {
      let _this = this
      let date = new Date().getTime()
      let setting = {
        randomTime: window.sessionStorage.getItem('randomTime'),
        beginTime: window.sessionStorage.getItem('beginTime'),
        endTime: window.sessionStorage.getItem('endTime')
      }
      data.rows.forEach(item => {
        if (item.bean.isUseAge == 1) {
          item.bean.ageMin = util.dateFormat(item.bean.ageMin,false)
          item.bean.ageMax = util.dateFormat(item.bean.ageMax,false)
        }
        item.bean.schedules = []
        if (item.bean.schedule) {
          var schedules = item.bean.schedule.split('<br>')
          item.bean.schedule = schedules[0]
          item.bean.schedules = schedules
        }
        _this.processStatus(item, setting, date)
      })
      if (_this.pageNo == 1) {
        _this.content = data.rows
      } else if (_this.pageNo > 1) {
        _this.content = _this.content.concat(data.rows)
      }
    },
    classList() {
      var _this = this
      console.log(_this.siteId)
      http.post(CONSTANT.CLASSLISTDO, {
        orgId: _this.siteId,
        area: _this.area,
        term: _this.term,
        spelId: _this.spelId,
        weekDay: _this.weekDay,
        pageNo: _this.pageNo,
        userId: _this.studentId
      }, (res) => {
        let data = res.data
        console.log('classList', data.rows)
        if (data.rows != null && data.rows.length > 0) {
          _this.classListResultHandle(data)
        } else {
          if (_this.pageNo > 1) {
            alert('没有更多了！')
            _this.pageNo--
            return
          } else {
            _this.content = []
          }
        }
      })
    },
    handleItemChangeArea (val) {
      var _this = this
      // 地区选择 动态获取
      if (_this.parentCodeArea != val[0]) {
        _this.parentCodeArea = val[0]
        _this.changeArea()
      }
    },
    changeArea () {
      // 区域 改成了 专业
      var _this = this
      http.post(CONSTANT.SPECIALTYLIST, {orgId: _this.siteId}, (res) => {
        res.data.forEach(item => {
          if (!item.parentId && item.status == '1') {
            var areaFirst = {}
            areaFirst.label = item.name
            areaFirst.value = item.id
            areaFirst.cities = []
            _this.optionsArea.push(areaFirst)
          }
        })
        res.data.forEach(item => {
          if (item.parentId) {
            _this.optionsArea.forEach(pItem => {
              if (item.parentId == pItem.value) {
                var areaSecond = {}
                areaSecond.label = item.name
                areaSecond.value = item.id
                pItem.cities.push(areaSecond)
              }
            })
          }
        })
        // if (_this.parentCodeArea == '0') {
        //   for (var a = 0; a < res.length; a++) {
        //     var areaFirst = {}
        //     areaFirst.label = res[a].name
        //     areaFirst.value = res[a].code
        //     areaFirst.cities = []
        //     _this.optionsArea.push(areaFirst)
        //   }
        // } else {
        //   var areaSeconds = []
        //   for (var aa = 0; aa < res.length; aa++) {
        //     var areaSecond = {}
        //     areaSecond.label = res[aa].name
        //     areaSecond.value = res[aa].code
        //     areaSeconds.push(areaSecond)
        //   }
        //   for (var aab = 0; aab < _this.optionsArea.length; aab++) {
        //     if (_this.optionsArea[aab].value == _this.parentCodeArea) {
        //       _this.optionsArea[aab].cities = areaSeconds
        //       return
        //     }
        //   }
        // }
      })
    },
    termlist() {
      // 学期
      var _this = this
      http.post(CONSTANT.ORGDICTLIST, {
        orgId: _this.siteId,
        type: 'edu_term'
      }, (res) => {
        res.data.forEach(item => {
          var termFirst = {}
          termFirst.label = item.label
          termFirst.value = item.value
          _this.optionsTerm.push(termFirst)
        })
      })
    },
    specialtylist() {
      // 专业 改成了  区域
      var _this = this
      http.post(CONSTANT.AREALIST, {orgId: _this.siteId}, (res) => {
        console.log('quyu', res)
        _this.spelName = res[0].resName
        for (var s = 0; s < res.length; s++) {
          var termFirst = {}
          termFirst.label = res[s].resName
          termFirst.value = res[s].resCode
          _this.optionsSpelId.push(termFirst)
        }
      })
    },
    weeklist() {
      // 时间段
      var _this = this
      http.post(CONSTANT.DICTLIST, {
        orgId: _this.siteId,
        type: 'sys_weekday'
      }, (res) => {
        res.data.forEach(item => {
          var weekFirst = {}
          weekFirst.label = item.label
          weekFirst.value = item.value
          _this.optionsWeekDay.push(weekFirst)
        })
      })
    },
    goCourseDetails(id,canApply) {
      if (canApply) {
        window.sessionStorage.setItem('event_id', id)// courseDetails
        this.$router.push({path: '/courseDetails'})
      }
    },
    processStatus(data, setting, currentTime) {
      if (data.bean.status == 3 && (data.bean.issueStatus == 1 || data.bean.issueStatus == 4)) {
        if (data.bean.recruitNum - data.bean.reserveNum > data.bean.applyUsedNum) {
          data.canApply = true;
          data.statusProperty = "报名进行中";
        } else {
          data.canApply = false;
          data.statusProperty = "名额已满";
        }
      } else if (data.bean.status == 3 && data.bean.issueStatus == 2) {
        data.canApply = false;
        if (data.bean.enrollType == 0) {
          data.statusProperty = "即报即得";
        } else if (data.bean.enrollType == 1) {
          data.statusProperty = "摇号中";
        } else if (data.bean.enrollType == 2) {
          data.statusProperty = "面试中";
        }
      } else if (data.bean.status == 3 && data.bean.issueStatus == 3) {
        data.canApply = false;
        data.statusProperty = "报名已关闭";
      }
      if (setting.beginTime > currentTime) {
        data.canApply = false;
        data.statusProperty = "报名未开始";
      }
      if (setting.endTime < currentTime) {
        data.canApply = false;
        data.statusProperty = "报名已结束";
      }
      // 是否已经到了预设的摇号时间
      if (setting.randomTime < currentTime) {
        // 摇号的班级进入摇号时间后不得再报名
        if (data.bean.enrollType == 1) {
          data.canApply = false;
          data.statusProperty = "摇号中";
        }
      }
    }
  }
}
